<template>
  <div id="app">
    <router-view></router-view>
  </div>
</template>

<script>
  import { Toast } from 'vant'
  import router from '@/router'
  import { getToken , removeToken } from '@/utils/auth'
  import { checkToken } from '@/api/common'

  export default {

    created() {
      //判断是否有token
      if(getToken()){
        this.timer();
      }else{
        router.push('/login');
      }
      
    },
    methods: {
      // 这是获取数据当前用户登录状态
      getData() {
        if( getToken() ){
          checkToken().then(
              res => {
                if( res.code == 200 && res.status == 'error' ){
                    Toast(res.message);
                    removeToken();
                    router.push('/login');
                }
              }
          );
        }
      },
      timer(){
        return setInterval(()=>{
          this.getData()
        },5000);
      }
      
    },
    destroyed() {
      clearInterval(this.timer());
    }
  }
</script>

<style lang="less">
body {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  margin: 0;
}
</style>
